﻿<%@ WebHandler Language="C#" Class="TableHandler" %>

using System;
using System.Web;
using System.Web.SessionState;
using Common;
using Service;
using Model.Table;
using System.Collections.Generic;
using System.Linq;
using Service.Table;

public class TableHandler : IHttpHandler, IRequiresSessionState
{
    private HttpContext context;
    public void ProcessRequest(HttpContext context)
    {
        this.context = context;
        context.Response.ContentType = "text/plain";

        string result = string.Empty;
        string method = context.Request["method"];
        try
        {
            switch (method)
            {
                case "GetTableDataByKey":    //获取redis中Excel表数据
                    result = GetTableDataByKey();
                    break;
                case "GetTableDataById":    //根据hashid 和 key，获取单条数据
                    result = GetTableDataById();
                    break;
                case "UpdateTableData": //更新redis中Excel表数据
                    result = UpdateTableData();
                    break;
                case "UpdateTableDataAll":    //全部更新Excel表数据
                    result = UpdateTableDataAll();
                    break;
                //case "GetTableObjectById":
                //    GetTableObjectById();
                //    break;
                //case "GetTableObjectList":
                //    GetTableObjectList();
                //    break;
                default: break;
            }
        }
        catch (Exception err)
        {
            context.Response.Write(err.ToString());
            return;
        }

        context.Response.Write(result);
    }

    //public void GetTableObjectById()
    //{
    //    string key = context.Request["key"];    //Excel中表名
    //    string id = context.Request["id"];
    //    if (!string.IsNullOrEmpty(key) && key != "" && !string.IsNullOrEmpty(id) && id != "")
    //    {
    //        TableBase tb = TableInfo.GetTableInfo(key);
    //        tb = TableService.GetTableObjectById<TableBase>(id);
    //    }
    //}

    //public void GetTableObjectList()
    //{
    //    string key = context.Request["key"];    //Table中表名
    //    if (!string.IsNullOrEmpty(key) && key != "")
    //    {
    //        TableBase tb = TableInfo.GetTableInfo(key);
    //        List<TableBase> list = TableService.GetTableObjectList<TableBase>();
    //    }
    //}


    /// <summary>
    /// 根据hashid 和 key，获取单条数据
    /// </summary>
    /// <returns></returns>
    public string GetTableDataById()
    {
        Dictionary<string, object> valueList = new Dictionary<string, object>();
        string key = context.Request["key"];    //Excel中表名
        string id = context.Request["id"];
        if (!string.IsNullOrEmpty(key))
        {
            //获取Excel配置
            TableConfig table = TableInfo.GetTableConfig.Where(p => p.SheetName == key).FirstOrDefault();
            valueList = TableService.GetTableDataById(table.RedisHashKey, id);
        }
        return MiniJSON.Json.Serialize(valueList);
    }

    /// <summary>
    /// 获取redis中Excel表数据
    /// </summary>
    /// <returns></returns>
    public string GetTableDataByKey()
    {
        //List<Dictionary<string, object>> valueList = new List<Dictionary<string, object>>();
        Dictionary<string, object> valueList = new Dictionary<string, object>();
        string key = context.Request["key"];    //Excel中表名
        if (!string.IsNullOrEmpty(key))
        {
            //获取Excel配置
            TableConfig table = TableInfo.GetTableConfig.Where(p => p.SheetName == key).FirstOrDefault();
            valueList = TableService.GetTableDataByKey(table.RedisHashKey);
        }
        return MiniJSON.Json.Serialize(valueList);
    }

    /// <summary>
    /// 更新redis中Excel表数据
    /// </summary>
    /// <returns></returns>
    public string UpdateTableData()
    {
        string result = string.Empty;
        string key = context.Request["key"];    //Excel中表名
        if (!string.IsNullOrEmpty(key))
        {
            //获取Excel配置
            TableConfig table = TableInfo.GetTableConfig.Where(p => p.SheetName == key).FirstOrDefault();
            if (TableService.UpdateTableData(table))
            {
                result = table.SheetName + "数据更新成功";
            }
        }
        return result;
    }

    /// <summary>
    /// 全部更新Excel表数据
    /// </summary>
    /// <returns></returns>
    public string UpdateTableDataAll()
    {
        string result = string.Empty;
        //获取Excel配置
        List<TableConfig> tableList = TableInfo.GetTableConfig;
        if (tableList != null && tableList.Any())
        {
            foreach (var item in tableList)
            {
                if (TableService.UpdateTableData(item))
                {
                    result += item.SheetName + "数据更新成功\n";
                }
            }

            //更新全部名称
            List<TableConfig> table_name_list = tableList.Where(p => p.Path == "table\\RandomName.xls").ToList();
            if (table_name_list != null && table_name_list.Any())
            {
                TableGivenname allname = new TableGivenname();
                if (TableService.UpdateTableGivennameData(allname.GetTableName(), table_name_list))
                {
                    result += "高级船员全部名称更新成功\n";
                }
            }
        }

        List<TableConfig> tableListOrderKey = TableInfo.GetTableConfigOrderKey;
        if (tableListOrderKey != null && tableListOrderKey.Any())
        {
            foreach (var item in tableListOrderKey)
            {
                if (TableService.UpdateTableDataOrderKey(item))
                {
                    result += item.SheetName + "数据更新成功\n";
                }
            }
        }
        return result;
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}